<template>
    <div class="msj_addsecond">
        <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">
        <i class="el-icon-s-home"></i>
        首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        <i class="el-icon-s-shop"></i> 
        活动管理
      </el-breadcrumb-item>
      <!-- 当前 -->
      <el-breadcrumb-item>秒杀管理</el-breadcrumb-item>
      <!-- 当前 -->
      <el-breadcrumb-item>{{ breadcrumb }}</el-breadcrumb-item>
    </el-breadcrumb>

    <!--  -->
    <div class="msj_addsecond_form">
        <!-- 返回按钮 -->
        <div style="font-size: 20px; font-weight: 700;">
            <i class="el-icon-arrow-left" @click="goback"></i>
        </div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="会场名" prop="name">
    <el-input v-model="ruleForm.name" style="width: 200px;" :keyup="ruleForm.name=ruleForm.name.replace(/[^\w\u4E00-\u9FA5]/g, '')"></el-input>
  </el-form-item>
  <el-form-item label="开始时间" required>
    <el-col :span="11">
      <el-form-item prop="date1">
        <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;"></el-date-picker>
      </el-form-item>
    </el-col>
  </el-form-item>
  <el-form-item label="自动取消" prop="type">
    <el-input v-model="ruleForm.type" style="width: 200px;" @blur="onblur" :keyup="ruleForm.type=ruleForm.type.replace(/\D/g,'')"></el-input>
    <p style="color: red; font-size: 13px; padding: 5px 0;">抢购成功,不付款多少秒之后,自动取消资格,默认120秒(2分钟)</p>
  </el-form-item>
  <el-form-item label="状态" prop="resource">
    <el-radio-group v-model="ruleForm.resource">
      <el-radio label="启用"></el-radio>
      <el-radio label="禁用"></el-radio>
    </el-radio-group>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
    <el-button @click="resetForm()">取消</el-button>
  </el-form-item>
</el-form>
    </div>
    </div>
</template>

<script>
import {addmsjhuichang,msjsethuichang} from '../../../utils/api'
export default {
    name: 'PhaemacyAddSecond',
    data() {
        return {
            breadcrumb:'添加会场',
            ruleForm: {
          name: '',
          date1: '',
          type: '120',
          resource: '启用',
        },
        second:{

        },
        rules: {
          name: [
            { required: true, message: '请输入会员名称', trigger: 'blur' },
          ],
          date1: [
            { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
          ],
          resource: [
            { required: true, message: '请选择活动资源', trigger: 'change' }
          ],
        }
        };
    },

    mounted() {
        let data=this.$route.params.add?this.$route.params:JSON.parse(sessionStorage.getItem('huichang'))
        this.breadcrumb=data.add?this.breadcrumb:'修改会场信息'
        this.second=data
        this.ruleForm.name=data.data.huiname
        this.ruleForm.date1=new Date(data.data.starttime) 
        this.ruleForm.type=data.data.zdquxiao 
        this.ruleForm.resource=data.data.state=='1'?'启用':'禁用'
    },

    methods: {
        // 返回 
        goback(){
            this.$router.go(-1)
        },
        submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {

            let obj={
                huiname:this.ruleForm.name,
                starttime:this.ruleForm.date1.toLocaleDateString().replaceAll('/','-'),
                times:this.second.add?(new Date().toLocaleString().replaceAll('/','-')):this.second.data.times,
                state:this.ruleForm.resource=='启用'?true:false,
                spnums:this.second.add?'0':this.second.data.spnums,
                zdquxiao:this.ruleForm.type
            }
            if(!this.second.add){
                obj.id=this.second.data.id
            }
            (this.second.add?addmsjhuichang(obj):msjsethuichang(obj)).then(res=>{
                this.$message({
                    message: res.data.msg,
                    type: 'success'
                })
                this.goback()
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm() {
        this.goback()
      },
      onblur(){
        this.ruleForm.type=this.ruleForm.type==''?'120':this.ruleForm.type
      }
    },
};
</script>

<style scoped>
*{
    margin: 0;
}
    .msj_addsecond_form,.msj_addsecond{
        width: 100%;
    }
    .msj_addsecond_form{
        padding: 15px 0 0;
    }
    .el-form-item{
        margin-bottom: 20px;
    }
</style>